package interceptcrossing.ui;

import interceptcrossing.Intercept;
import interceptcrossing.Point;
import interceptcrossing.ui.command.AddInterceptCommand;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddInterceptDialog extends JDialog {

	private MainWindowDirector windowDirector;

	private JSpinner beginAbscissaSpinner;
	private JSpinner beginOrdinateSpinner;
	private JSpinner endAbscissaSpinner;
	private JSpinner endOrdinateSpinner;
	private JButton addButton;
	private JButton cancelButton;
	private Frame owner;

	public AddInterceptDialog(Frame owner, MainWindowDirector director) {
		super(owner, "Добавление отрезка", true);

		this.owner = owner;
		this.windowDirector = director;

		double value = 1.5;
		double min = -1e+5;
		double max = 1e+5;

		this.beginAbscissaSpinner = new JSpinner(
			new SpinnerNumberModel(value, min, max, 0.5));
		this.beginOrdinateSpinner = new JSpinner(
			new SpinnerNumberModel(value, min, max, 0.5));
		this.endAbscissaSpinner = new JSpinner(
			new SpinnerNumberModel(value, min, max, 0.5));
		this.endOrdinateSpinner = new JSpinner(
			new SpinnerNumberModel(value, min, max, 0.5));
		this.addButton = new JButton("Добавить");
		this.cancelButton = new JButton("Отмена");

		this.initLayout();
		this.initActions();

		super.pack();
		super.setMinimumSize(super.getSize());
		super.setLocationRelativeTo(owner);
	}

	private void initLayout() {
		Container contentPane = super.getContentPane();
		contentPane.setLayout(new GridBagLayout());

		JPanel beginPanel = new JPanel();
		beginPanel.setBorder(new TitledBorder("Координаты начала"));
		beginPanel.setLayout(new GridLayout(2, 2, 10, 10));
		beginPanel.add(new JLabel("X:"));
		beginPanel.add(this.beginAbscissaSpinner);
		beginPanel.add(new JLabel("Y:"));
		beginPanel.add(this.beginOrdinateSpinner);

		JPanel endPanel = new JPanel();
		endPanel.setBorder(new TitledBorder("Координаты конца"));
		endPanel.setLayout(new GridLayout(2, 2, 10, 10));
		endPanel.add(new JLabel("X:"));
		endPanel.add(this.endAbscissaSpinner);
		endPanel.add(new JLabel("Y:"));
		endPanel.add(this.endOrdinateSpinner);

		contentPane.add(beginPanel, new GridBagConstraints(1, 1, 2, 1, 1.0,
			1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
			new Insets(10, 10, 10, 10), 0, 0));

		contentPane.add(endPanel, new GridBagConstraints(1, 2, 2, 1, 1.0,
			1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
			new Insets(10, 10, 10, 10), 0, 0));

		contentPane.add(this.addButton, new GridBagConstraints(1, 3, 1, 1, 0.5,
			0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
			new Insets(10, 10, 10, 10), 0, 0));

		contentPane.add(this.cancelButton, new GridBagConstraints(2, 3, 1, 1, 0.5,
			0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
			new Insets(10, 10, 10, 10), 0, 0));
	}

	private void initActions() {
		this.addButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if (AddInterceptDialog.this.addIntercept()) {
					AddInterceptDialog.this.setVisible(false);
				}
			}
		});

		this.cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				AddInterceptDialog.this.setVisible(false);
			}
		});
	}

	private boolean addIntercept() {
		Point begin = new Point((Double) this.beginAbscissaSpinner.getValue(),
			(Double) this.beginOrdinateSpinner.getValue());
		Point end = new Point((Double) this.endAbscissaSpinner.getValue(),
			(Double) this.endOrdinateSpinner.getValue());

		Intercept intercept = new Intercept(begin, end);

		if (this.windowDirector.hasIntercept(intercept)) {
			JOptionPane.showMessageDialog(this.owner,
				"Отрезок с такими данными уже был добавлен ранее.",
				"Уведомление", JOptionPane.INFORMATION_MESSAGE);
			return false;
		}

		this.windowDirector.getCommandManager().doCommand(
			new AddInterceptCommand(this.windowDirector, intercept));
		return true;
	}
}
